package tmall.common.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;

/**
 * 控制器基类
 *
 * @author zhangservlet
 * @date 2020/12/11
 */
@ApiIgnore
public class BaseController {

    protected final Logger logger = LogManager.getLogger(BaseController.class);

    /**
     * 通用上传图片方法
     * @param filePath
     * @param file
     * @throws IOException
     */
    protected void uploadImg(String filePath, MultipartFile file) throws IOException {
        File localFile = new File(filePath);
        if (!localFile.getParentFile().exists()) {
            localFile.getParentFile().mkdirs();
        }
        logger.info("文件上传路径：" + filePath);
        logger.info("文件上传中...");
        file.transferTo(localFile);
        logger.info("文件上传完成");
    }
}
